const {
    addcomments,
    getcommentlist,
    getnum,
    addreplycomments,
    setstrs,
    getliuyancommentsnum,
    getliuyancomments,
    setdanmu,
    getdanmus
} = require("../serve/comments.js")
const Response = require("../util/respanse.js")
/* 添加评论 */
const addcomment = async (req, res) => {
    try {
        let { type, content, forid, username, usersiteurl, usercover } = req.body
        if (!usercover) {
            usercover = "http://imgfile.xxkdjy.top/2024011919245858472.jpg"
        }
        let result = await addcomments({ type, content, forid, username, usersiteurl, usercover })
        if (result.code) {
            Response.successresponse(res, {
                code: 200,
                message: "成功",
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "评论失败"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
/* 获取评论 */
const getcomment = async (req, res) => {
    try {
        let { type, forid } = req.query
        console.log({ type, forid });
        if (type && forid) {
            type = parseInt(type)
            let result = await getcommentlist({ type, forid })
            if (result.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "获取评论成功",
                    data: result.data
                })
            } else {
                Response.successresponse(res, {
                    code: 200,
                    message: result.msg,
                    data: result.data
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "参数错误"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
/* 获取评论数量 */
const getcommentunm = async (req, res) => {
    try {
        let { type, forid } = req.query
        let result = await getnum({ type, forid })
        if (result.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: result.data
            })
        } else {
            Response.successresponse(res, {
                code: 200,
                message: "暂无数据",
                data: result.data
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
/* 回复评论 */
const replycomments = async (req, res) => {
    try {
        let { type, content, forid, username, usersiteurl, usercover, fatherid, fromid, fromname } = req.body
        if (!usercover) {
            usercover = "http://imgfile.xxkdjy.top/2024011919245858472.jpg"
        }
        if (!fatherid) {
            Response.errorresponse(res, {
                code: 400,
                msg: "参数错误"
            })
        } else {
            let result = await addreplycomments({ type, content, forid, username, usersiteurl, usercover, fatherid, fromid, fromname })
            if (result.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "成功",
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "评论失败"
                })
            }
        }

    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
const addstrs = async (req, res) => {
    try {
        let { id, type, flag } = req.body
        if (!id || !type || !flag) {
            Response.errorresponse(res, {
                code: 400,
                msg: "参数错误"
            })
        }
        let result = await setstrs({ id, type, flag })
        console.log(result.msg);
        if (result.code) {
            Response.successresponse(res, {
                code: 200,
                message: "成功",
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "失败"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
/* 留言区的评论  type: props.typeobj.type,
            forid: props.typeobj.id,
            page:props.pageinfo.page,
            pageSize:props.pageinfo.pageSize*/
const getliuyan = async (req, res) => {
    try {
        let { type, forid, page, pageSize } = req.query
        let result = await getliuyancomments({ type, forid, page, pageSize })
        if (result.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取评论成功",
                data: result.data
            })
        } else {
            Response.successresponse(res, {
                code: 200,
                message: result.msg,
                data: result.data
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
/* 获取留言 */
const getliuyannums = async (req, res) => {
    let ress = await getliuyancommentsnum({ type: "3", forid: "1" })
    if (ress.code) {
        Response.successresponse(res, {
            code: 200,
            message: "成功",
            data: ress.data
        })
    }
}
/* 添加弹幕 */
const adddanmu = async (req, res) => {
    try {
        let { content } = req.body
        console.log(content);
        let ress = await setdanmu(content)
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: "评论成功",
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "失败"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
/* 获取弹幕 */
const getdanmu =async (req,res)=>{
    try {
        let ress = await getdanmus()
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: "成功",
                data: ress.data
            })
        }else{
            Response.errorresponse(res, {
                code: 400,
                msg: "暂无数据"
            })
        }
    }catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
        })
    }
}
module.exports = {
    addcomment,
    getcomment,
    getcommentunm,
    replycomments,
    addstrs,
    getliuyan,
    getliuyannums,
    adddanmu,
    getdanmu
}